import getProp from './prop'
import { validateRequired } from '../rules/utils'
import { validateArrLen } from '../rules/array'

const CHECK_TIPS = '选择'
/**
 * 获取验证字段
 * @param {*} { fields }
 * @return {*} rule
 */
function _getRules ({ fields }) {
  const { label, required } = getProp({ fields })
  const { verification, checkValue } = fields
  const { type, max_count = 0, min_count = 0 } = verification

  return {
    rules: [
      validateRequired(type, required, `请${CHECK_TIPS}${label}！`),
      validateArrLen({ min_count: min_count || 0, max_count: max_count || 0 }, CHECK_TIPS),
    ],
    minCount: min_count || 0,
    maxCount: max_count || 0,
    checkValue,
  }
}

/**
 * @export 获取checkbox类型表单属性
 * @param {*} { fields }
 * @return {*} prop
 */
export default function getCheckboxProp ({ fields }) {
  const {
    choice,
    defaultProps,
  } = fields

  const rules = _getRules({ fields })

  const prop = getProp({ fields })

  return {
    ...prop,
    choice: choice || [],
    defaultProps,
    ...rules,
  }
}
